<?php

class NCore_View
{
	protected $name;
	protected $databindingMethods = array();
	protected $databoundMethods = array();
	protected $rowDatabindingMethods = array();
	protected $rowDataboundMethods = array();
	protected $mappings = array();
	protected $whitelists = array();
	protected $markup = NULL;
	
	function __construct($name, $instance_vars = NULL)
	{
		$this->withName($name);
	}
	public function withName($name)
	{
		if(!preg_match(N::REGEX_KEY, $name))
		{
			throw new Exception('View names must begin with a letter; contain letters, numbers, or the underscore; and be no longer than 40 characters.');
		}

		$this->name = $name;
		return $this;
	}
	public function getName()
	{
		return $this->name;
	}
	public function withMappings($arr)
	{
		$this->mappings = $arr;
		return $this;
	}
	public function getMappings($dataset)
	{
		$defaultMappings = $this->getDefaultMappings($dataset);
		return array_merge($defaultMappings, $this->mappings);
	}
	public function getDefaultMappings($dataset)
	{
		$defaultMappings = array();
			
		foreach($dataset as $row)
		{
			foreach($row as $key=>$value)
			{
				$defaultMappings[$key] = '{'.$key.'}';
			}
			break;
		}
		
		return $defaultMappings;
	}
	public function addWhitelist($key, $arr)
	{
		$this->whitelists[$key] = $arr;
		return $this;
	}
	public function getWhitelists()
	{
		return $this->whitelists;
	}
	public function withMarkup($markup)
	{
		$this->markup = $markup;
		return $this;
	}
	public function getMarkup()
	{
		return $this->markup;
	}
	public function addDatabindingMethod($obj, $method)
	{
		$this->databindingMethods[] = new NCore_EventCallback($obj, $method);
		return $this;
	}
	public function getDatabindingMethods()
	{
		return $this->databindingMethods;
	}	
	public function addDataboundMethod($obj, $method)
	{
		$this->databoundMethods[] = new NCore_EventCallback($obj, $method);
		return $this;
	}
	public function getDataboundMethods()
	{
		return $this->databoundMethods;
	}
	public function addRowDatabindingMethod($obj, $method)
	{
		$this->rowDatabindingMethods[] = new NCore_EventCallback($obj, $method);
		return $this;
	}
	public function getRowDatabindingMethods()
	{
		return $this->rowDatabindingMethods;
	}
	public function addRowDataboundMethod($obj, $method)
	{
		$this->rowDataboundMethods[] = new NCore_EventCallback($obj, $method);
		return $this;
	}
	public function getRowDataboundMethods()
	{
		return $this->rowDataboundMethods;
	}
	function __call($method, $args)
	{
		throw new Exception('There is no method called \''.$method.'\'() on the view \''.$this->getName().'\'.');
	}
}

?>